package com.inool.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.inool.model.Course;
import com.inool.mapper.CourseMapper;
import com.inool.service.CourseService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

    @Autowired
    private CourseMapper courseMapper;

    @Override
    public Course addCourse(Course course) {
        save(course);
        return course;
    }

    @Override
    public Course updateCourse(String id, Course course) {
        course.setCourseId(id);
        updateById(course);
        return course;
    }

    @Override
    public void deleteCourse(String id) {
        removeById(id);
    }

    @Override
    public Course getCourseById(String id) {
        return getById(id);
    }

    @Override
    public List<Course> getAllCourses() {
        return list();
    }

    @Override
    public Page<Course> pageCourses(long current, long size, String courseName, Integer credits) {
        Page<Course> page = new Page<>(current, size);
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        if (courseName != null && !courseName.equals("")) {
            wrapper.like("course_name", courseName);
        }
        if (credits != null && !credits.equals("")) {
            wrapper.eq("credits", credits);
        }
        this.page(page, wrapper);
        return page;
    }

}    